import os

OPENHARMONY_CODE_PATH = "/home/lmxxf/K1_OH5.0/source"
exclude_dirs = set(['out', '.repo', '.git', 'prebuilts', 'third_party'])
required_exts_tuple = tuple([".c", ".cpp", ".h", ".hpp", ".cc", ".gn", ".gni", ".py", ".java", ".json", ".json5", ".xml", ".hcs", ".md"])
file_count = 0

print(f"--- Starting Walk from: {OPENHARMONY_CODE_PATH} ---")

# 使用 os.walk 遍历目录
for dirpath, dirnames, filenames in os.walk(OPENHARMONY_CODE_PATH, topdown=True):
    print(f"\n>>> Walking into: {dirpath}")
    print(f"    Original subdirs: {dirnames}")
    
    # 过滤目录的逻辑
    pruned_dirnames = [d for d in dirnames if d not in exclude_dirs and 'test' not in d.lower()]
    
    # 检查哪些目录被排除了
    excluded_this_level = set(dirnames) - set(pruned_dirnames)
    if excluded_this_level:
        print(f"    Excluded subdirs this level: {sorted(list(excluded_this_level))}")

    # 修改 dirnames 列表，os.walk 将不会进入被排除的目录
    dirnames[:] = pruned_dirnames
    print(f"    Will continue into subdirs: {dirnames}")
    
    # 在当前目录查找符合条件的文件
    found_in_dir = 0
    for filename in filenames:
        if filename.endswith(required_exts_tuple):
            file_count += 1
            found_in_dir += 1
            # print(f"        Found matching file: {filename}") # 暂时注释掉，避免过多输出
    
    if found_in_dir > 0:
        print(f"    +++ Found {found_in_dir} matching files in this directory. +++")

print(f"\n--- Walk Finished. Total matching files found: {file_count} ---")
